package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Carroceria;
import negModelExcep.*;

public class CarroceriaTest extends TestCase {

	Carroceria carr1,carr2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		carr1 = new Carroceria (100,500,3,new String("carr1"),new AlgoPesos(100),true,5,1,10,10,80,100);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","carroceria");
		
		e.setAttribute("maxVU", ""+ carr1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ carr1.getVidaUtil());
		e.setAttribute("peso", ""+ carr1.getPeso());
		e.setAttribute("aportevmax", ""+ carr1.aportevmax());
		e.setAttribute("detalle", carr1.getDetalle());
		e.setAttribute("precio", ""+(carr1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+carr1.esRemovible());
		e.setAttribute("aerodinamia", ""+carr1.getAerodinamia());
		
		e.setAttribute("altoCabina", ""+carr1.getAltoCabina());
		e.setAttribute("altoFrente", ""+carr1.getAltoFrente());
		e.setAttribute("largo", ""+carr1.getLargo());
		e.setAttribute("ancho", ""+carr1.getAncho());
		e.setAttribute("color", ""+carr1.getColor());
	    
	}

	public void testGastar() {
		try {
			carr1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(99,carr1.getVidaUtil(),0.1);
	}

	//coef=100/((this.largo*this.ancho)*(this.altoCabina-this.altoFrente))
	public void testFuncionar() {
		try {
			assertEquals(0.05,carr1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
	}

	public void testGetLargo() {
		assertEquals(10,carr1.getLargo(),0);
		
	}

	public void testGetAltoCabina() {
		assertEquals(100,carr1.getAltoCabina(),0);
		
	}

	public void testGetAncho() {
		assertEquals(10,carr1.getAncho(),0);
		
	}

	public void testGetColor() {
		assertEquals(1,carr1.getColor(),0);
		
	}

	public void testGetAltoFrente() {
		assertEquals(80,carr1.getAltoFrente(),0);
		
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=carr1.toXml(doc);
		
		assertEquals(carr1.getAltoFrente(),Double.parseDouble(e.getAttribute("altoFrente")),0.0);
		assertEquals(carr1.getAltoCabina(),Double.parseDouble(e.getAttribute("altoCabina")),0.0);
		assertEquals(carr1.getLargo(),Double.parseDouble(e.getAttribute("largo")),0.0);
		assertEquals(carr1.getAncho(),Double.parseDouble (e.getAttribute("ancho")),0.0);
		assertEquals(carr1.getColor (),Integer.parseInt(e.getAttribute("color")));
		
		
		assertEquals(carr1.getAerodinamia(),Integer.parseInt(e.getAttribute("aerodinamia")));
		assertEquals((carr1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(carr1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(carr1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(carr1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(carr1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testCarroceria()throws Exception {
		
		carr2=new Carroceria(e);
				
		assertEquals(carr2.getAltoFrente(),Double.parseDouble(e.getAttribute("altoFrente")),0.0);
		assertEquals(carr2.getAltoCabina(),Double.parseDouble(e.getAttribute("altoCabina")),0.0);
		assertEquals(carr2.getLargo(),Double.parseDouble(e.getAttribute("largo")),0.0);
		assertEquals(carr2.getAncho(),Double.parseDouble (e.getAttribute("ancho")),0.0);
		assertEquals(carr2.getColor (),Integer.parseInt(e.getAttribute("color")));
		
		assertEquals(carr2.getAerodinamia(),Integer.parseInt(e.getAttribute("aerodinamia")));
		assertEquals((carr2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(carr2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(carr2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(carr2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(carr2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}

}
